package com.license.servlets;

import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;

import com.license.common.util.Constants;
import com.license.logic.classification.NaiveBayesClassifier;
import com.license.presentation.recomandation.RecomandationOptimization;

/**
 * Servlet implementation class RecomandationOptimizationServlet
 */
public class RecomandOptServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RecomandationOptimization recOptimization;
	private static String employeeName;
	
    public RecomandOptServlet() {
        super();
        recOptimization = new RecomandationOptimization();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("GET for RecomandOptServlet");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		String operation = request.getParameter("operationRecomandation");
		 employeeName = request.getParameter("employeeName");
		
		String individNumberString = request.getParameter("individNumber");
		String iterationNumberString = request.getParameter("iterationNumber");
		
		int individNumber = individNumberString == null || "".equals(individNumberString)? 0 :Integer.parseInt(individNumberString);
		int iterationNumber = iterationNumberString == null || "".equals(iterationNumberString)? 0 :Integer.parseInt(iterationNumberString);
		int paretoOptimalMethod = Integer.parseInt(request.getParameter("paretoOptimalMethod"));
		if ("PSO".equals(operation)){
			try {
				request.getSession().setAttribute("constraintMethod", Constants.JESS_CONSTRAINT_METHOD);
				recOptimization.recomandUsingPSO(employeeName + Constants.XML_EXTENSION, individNumber, iterationNumber, paretoOptimalMethod);
				response.sendRedirect("../faces/JSFfiles/MenuPage.jsp");
				return;
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if ("CSO".equals(operation)){
			try {
				request.getSession().setAttribute("constraintMethod", Constants.XML_CONSTRAINT_MENTHOD);
				recOptimization.recomandUsingClonalSelection(employeeName + Constants.XML_EXTENSION, individNumber, iterationNumber, paretoOptimalMethod);
				response.sendRedirect("../faces/JSFfiles/MenuPage.jsp");
				return;
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public Map printLifeStyleRecomandation(HttpServletRequest request, HttpServletResponse response) {
		return recOptimization.getLifeStyleRecomandation();
	}
	
	public String getFoodDistribution(HttpServletRequest request, HttpServletResponse response) {
		return recOptimization.getFoodClassifiedNumbers();
	}
	
	public Map classify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return recOptimization.classify(employeeName);
	}
	
	public Map getDetails(HttpServletRequest request, HttpServletResponse response){
		String constraintMethod = request.getSession().getAttribute("constraintMethod").toString();
		return recOptimization.getResultCalory(constraintMethod);
	}

}
